package com.alan.socket;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * 简单客户端
 * Created by Alan on 2017/8/5.
 */
public class Client {

    public static void main(String[] args) {
        //1.创建一个客户端对象
        Socket socket = null;
        Scanner input = new Scanner(System.in);
        try {
            socket = new Socket("127.0.0.1",8888);
            //通过Socket对象得到输入输出流
            InputStream is = socket.getInputStream();
            OutputStream os = socket.getOutputStream();

            //将字节流转化为字符流
            PrintWriter pw = new PrintWriter(os,true);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            while (true){
                //从控制台接受发送的信息
                String info = input.nextLine();
                pw.println("客户端说："+info);

                //客户端接收消息
                String in = br.readLine();
                System.out.println(in);

            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
